export function sleep(t: number): Promise<any> {
  return new Promise<any>((resolve) => {
    setTimeout(resolve, t)
  })
}

export async function keepTrying(
  f: () => boolean,
  duration: number,
  interval: number = 100
): Promise<any> {
  const t0 = Date.now()
  // eslint-disable-next-line no-constant-condition
  while (true) {
    if (f() === true) {
      return
    }
    const t1 = Date.now()
    if (t1 - t0 > duration) {
      break
    }
    await sleep(interval)
  }
  throw Error('failed!')
}
